package cn.ffcs.uec.corp.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.corp.action.bean.UecPsnCorpEditBean;
import cn.ffcs.uec.corp.manager.UecPsnManager;
import cn.ffcs.uec.corp.model.UecPsnCorp;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecPsnCorpEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecPsnCorpEditBean bean = new UecPsnCorpEditBean();

	private String opType;

	@Resource
	private UecPsnManager uecPsnCorpManager;
	
	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	private UecPsnCorp uecPsnCorp;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
		
		
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecPsnCorpEditWin() throws Exception {
		this.bindCombobox();
		System.out.println(arg.get("opType").toString());
		opType = (String) arg.get("opType");
		if (opType.equals("add")) {
			bean.getUecPsnCorpEditWin().setTitle("外包公司新增");
			bean.getUecPsnCorpEditWin().setWidth("40%");
			
		} else if (opType.equals("edit")) {
			bean.getUecPsnCorpEditWin().setTitle("外包公司修改");
			PubUtil.fillBeanFromPo(arg.get("uecPsnCorp"), bean);
		}
	}
	
	/**
	 * 检测是否存在该员工
	 * @throws InterruptedException 
	 */
	public void onCheck() throws InterruptedException {
		/*uecPsnCorp = new UecPsnCorp();
		PubUtil.fillPoFromBean(bean, uecPsnCorp);
		//uecPsnCorpManager.findUecPsnCorp(uecPsnCorp);
		if (uecPsnCorpManager.findUecPsnCorp(uecPsnCorp) != null) {
			Messagebox.show("该员工已存在");
		} else {
			//bean.getUecPsnCorpEditWin().setWidth("60%");
			bean.getTestPsnCorp().setVisible(false);
			bean.getSavePsnCorp().setVisible(true);
		}*/
	}
	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecPsnCorp = UecPsnCorp.newInstance();
		
		if (opType.equals("add")) {
			// 保存员工
			PubUtil.fillPoFromEditorBean(uecPsnCorp, bean);
			//uecPsnCorp.setId(null);
			uecPsnCorp.addOnly();
			Events.postEvent("onOK", this.self, uecPsnCorp);
			onCancel();
			Messagebox.show("新增成功！");
		} 
		if (opType.equals("edit")) {
			PubUtil.fillPoFromEditorBean(uecPsnCorp, bean);
			uecPsnCorp.setId((Long)arg.get("id"));
			uecPsnCorp.setStatusCd("1000");
			DateFormatter();
			uecPsnCorp.updateOnly();
			Events.postEvent("onOK", this.self, uecPsnCorp);
			onCancel();
			Messagebox.show("更新成功！");
		}
		
		
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecPsnCorpEditWin().onClose();
	}
	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		// 绑定性别sex:
	}
	/**
	 * 校验
	 * @return
	 */
	private String checkUacStaffData() {
		// BaseInfo Check
		/*if (StrUtil.isNullOrEmpty(bean.getPsnName().getValue())) {
			return "请填写人员名";
		} else if (StrUtil.checkBlank(bean.getPsnName().getValue())) {
			return "人员名中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getIdNum().getValue())) {
			return "请填写证件号码";
		} else if (StrUtil.checkBlank(bean.getIdNum().getValue())) {
			return "证件号码中有空格";
		} */
		
		// IDCARD Real Name Check
		/*CertIdVarBaseInfo certIdVarBaseInfo = certIdVarBaseInfoManager
				.getCertIdVarInfo(bean.getPsnName()getValue(), bean.getIdNum()getValue());
		if (certIdVarBaseInfo) {
			String idNum = bean.getCertNumber().getValue();
			String idName = bean.getCertName().getValue();
			try {
				boolean isRealName = CertUtil.checkIdCard(idNum, idName);

				if (!isRealName) {
					return "实名认证未通过";
				}
			} catch (Exception e) {
				return "调用国政通接口失败";
			}

		}*/

		return null;
	}
	public void DateFormatter() {
		/*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getBirthDate().getValue())) {
			// 出生日期
			uecPsnCorp.setBirthDate(formatter.format(bean.getBirthDate()
					.getValue()));
		}*/
	}
}
